
{% extends 'base_main_goods.html' %}

{% block content %}
<div class="breadcrumb">
		<a href="{% url 'goods:index' %}">全部分类</a>
		<span>></span>
		<a href="#">{{ category_name }}</a>
		<span>></span>
		<a href="">商品详情</a>
	</div>

	<div class="goods_detail_con clearfix">
		<div class="goods_detail_pic fl"><img src='/media/{{ goods.goods_front_image }}'></div>

		<div class="goods_detail_list fr">
			<h3>{{ goods.name }}</h3>
			<p>{{ goods.goods_brief }}</p>
			<div class="prize_bar">
				<span class="show_pirze">¥<em>{{ goods.shop_price }}</em></span>
				<span class="show_unit">单  位：500g</span>
			</div>
			<div class="goods_num clearfix">
				<div class="num_name fl">数 量：</div>
				<div class="num_add fl">
					<input type="text" class="num_show fl" value="{{ number }}">
					<a href="javascript:;" onclick="add_number({{ goods.shop_price }})" class="add fr">+</a>
					<a href="javascript:;" onclick="sub_number({{ goods.shop_price }})" class="minus fr">-</a>
				</div>
                <script>
                    function add_number(price) {
                    {#    修改商品个数和商品的总价   #}
                        var num = $('.num_show').val();
                        var new_num = parseInt(num) + 1 ;
                        var total_price = price* new_num ;
                        {# 修改总价 #}
                        $('.total').html('<em>'+'总价：'+ total_price.toFixed(1) + ' 元'+'</em>');
                    {#    修改商品个数  #}
                       $('.num_show').val(new_num);
                    }
                    function sub_number(price) {
                        var num = $('.num_show').val();
                        var new_num = parseInt(num)-1;
                        if(new_num > 0 ){
                            {#    总价 #}
                        var total_price = new_num * price;
                        $('.total').html('<em>'+'总价：'+ total_price.toFixed(1) + ' 元'+'</em>');
                    {#    修改数量 #}
                        $('.num_show').val(new_num)
                        }
                    }
                </script>
			</div>
			<div class="total"><em>总价：{{ goods.shop_price }} 元</em></div>
			<div class="operate_btn">
				<a href="javascript:;" onclick="add_cart({{ goods.id }},flag=true)" class="buy_btn">立即购买</a>
				<a href="javascript:;" onclick="add_cart({{ goods.id }})" class="add_cart" id="add_cart">加入购物车</a>
			</div>
            {% csrf_token %}
            <script>
                function add_cart(goods_id,flag=false) {
                    var goods_num = $('.num_show').val();
                    var csrf = $('input[name="csrfmiddlewaretoken"]').val();
                    $.ajax({
                        url:'/carts/add_cart/',
                        type:'POST',
                        dataType:'json',
                        headers:{'X-CSRFToken':csrf},
                        data:{'goods_id':goods_id,'goods_num':goods_num},
                        success:function (data) {
                                if(data.code == 200){
                                    $('#show_count').html(data.goods_count)
                                }
{#为什么要用ajax，可以局部刷新，不用手动F5就会更新新的内容，但是变动的值是暂时显示的，所以也要在那个数值处传变量，永久显示 #}
                                if(flag){
                                    window.location.href='{% url 'carts:cart' %}'
                                }
                        },
                        error:function (data) {
                            alert('添加商品失败')
                        },
                    })

                }
            </script>
		</div>
	</div>

	<div class="main_wrap clearfix">
		<div class="l_wrap fl clearfix">
			<div class="new_goods">
				<h3>新品推荐</h3>
				<ul>
					<li>
						<a href="#"><img src="images/goods/goods001.jpg"></a>
						<h4><a href="#">进口柠檬</a></h4>
						<div class="prize">￥3.90</div>
					</li>
					<li>
						<a href="#"><img src="images/goods/goods002.jpg"></a>
						<h4><a href="#">玫瑰香葡萄</a></h4>
						<div class="prize">￥16.80</div>
					</li>
				</ul>
			</div>
		</div>

		<div class="r_wrap fr clearfix">
			<ul class="detail_tab clearfix">
				<li class="active">商品介绍</li>
				<li>评论</li>
			</ul>

			<div class="tab_content">
				<dl>
					<dt>商品详情：</dt>
					<dd>{{ goods.goods_desc | safe }}</dd>
				</dl>
			</div>

		</div>
	</div>

{% endblock %}

{% block js %}
    {{ block.super }}
    <script>
    $(function () {
        $('input[type="text"]').on({'input':function () {
                    {#    修改商品个数和商品的总价   #}
                var num = ($('.num_show').val());
                num = num.replace(/\D/g,'');
                num = parseInt(num);
                if(num>=0){
                        if(num == 0){
                            $('.num_show').val(1);
                            num = 1;
                       }
                    var price = $('.show_pirze>em').text();
                    var total_price = price* num ;
                    {# 修改总价 #}
                    $('.total').html('<em>'+'总价：'+ total_price.toFixed(1) + ' 元'+'</em>');
                {#    修改商品个数  #}
                   $('.num_show').val(num);
                }else{
                     $('.total').html('<em>'+'总价：'+ 0 + ' 元'+'</em>');
                }
        },'blur':function () {
                var num = $('.num_show').val();
                if(num == ''){
                    $('.num_show').val(1);
                    num = 1;
                    var price = $('.show_pirze>em').text();
                    var total_price = parseInt(price* num);
                    {# 修改总价 #}
                    $('.total').html('<em>'+'总价：'+ total_price.toFixed(1) + ' 元'+'</em>');
                {#    修改商品个数  #}
                   $('.num_show').val(num);
                }
            }})
    })



    </script>
{% endblock %}